/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.ui.controls.gallery
{		
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	public class Gallery extends Sprite
	{
		private var _galleryType:int;
		
		private var _itemsCount:int;
		private var _position:int;
		
		private var background:Sprite;		
		private var content:GalleryBox;	
		
		private var itemsArrayAll:Vector.<GalleryItem>;
		private var itemsArray:Vector.<GalleryItem>;
		private var itemsToDraw:Vector.<GalleryItem>;
		
		private var galleryFilter:GalleryFilter;
		
		public function Gallery(BackGround:Class, itemsCount:int, type:int = 2)
		{			
			background = new BackGround();
			addChild(background);			
			
			this._itemsCount = itemsCount;			
			
			content = new GalleryBox(type);
			
			background.addChild(content);
			
			_position = 0;			
			
			itemsArrayAll = new Vector.<GalleryItem>();
			itemsArray = new Vector.<GalleryItem>();
			itemsToDraw = new Vector.<GalleryItem>();
			
			addEventListener(Event.ADDED_TO_STAGE, addedToSage);			
		}
		
		public function SerchTextItems(FieldName:String, value:String):void{
			value = value.toLowerCase();
			itemsArray = new Vector.<GalleryItem>();
			_position = 0;
			
			for each(var item:GalleryItem in itemsArrayAll){
				if(item.hasOwnProperty(FieldName)){
					var itemValue:String = item[FieldName].toString();
					itemValue = itemValue.toLowerCase();
					if(itemValue.search(value) >= 0){
						itemsArray.push(item);
					}
					
				}									
			}
			
			update();
		}
		
		public function get itemsSeparator():Point{
			return content.itemsSeparator;
		}
		
		public function set itemsSeparator(value:Point):void{
			content.itemsSeparator = value;
		}
		
		public function get gallerySize():Point{
			return content.size;
		}
		
		public function set gallerySize(value:Point):void{
			content.size = value;
		}
		
		public function get itemsArrayCount():int{
			return itemsArray.length;
		}
		
		
		public function get itemsCount():int{
			return _itemsCount;
		}
		
		public function set itemsCount(value:int):void{
			_itemsCount = value;
		}
		
		public function get position():int{
			return _position;
		}
		
		public function set position(value:int):void{
			_position = value;
		}
		
		public function contentContainerMoveTo(point:Point):void{
			if(point && content){
				content.x = point.x;
				content.y = point.y;
			}
		}
		
		private function removedFromStage(event:Event):void{
			clear();
			removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
			addEventListener(Event.ADDED_TO_STAGE, addedToSage);
			
		}
		private function addedToSage(event:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE, addedToSage);
			addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
		}
		
		
		public function setSerchFilter(filter:GalleryFilter):void{
			if((galleryFilter)&&(filter)){
				if(galleryFilter.types == filter.types){
					return;
				}
			}			
			galleryFilter = filter;
			
			serch();					
		}
		
		protected function serch():void{
			if(!galleryFilter){
				itemsArray = itemsArrayAll;
			}else{
				itemsArray = new Vector.<GalleryItem>();
				_position = 0;
				for each(var item:GalleryItem in itemsArrayAll){
					for each(var type:String in galleryFilter.types){
						if(item.type == type){
							itemsArray.push(item);
							break;
						}else{
							//							item.destroy();
						}
					}					
				}
			}
			update();
		}	
			
		public function clear():void{
			while(content.numChildren){
				content.removeChildAt(0);
			}
			for each(var item:GalleryItem in itemsArray){
				item.destroy();
			}
			
			itemsArrayAll = new Vector.<GalleryItem>();
			itemsArray = new Vector.<GalleryItem>();
			itemsToDraw = new Vector.<GalleryItem>();
			
			disableLeft();			
			disableRight();			
		}
		
		protected function disableLeft():void{
			
		}	
		
		protected function disableRight():void{
			
		}
		
		protected function enabledLeft():void{
			
		}
		
		protected function enabledRight():void{
			
		}
		
		public function addItems(items:Array):void{
			clear();
			for each(var item:GalleryItem in items){
				itemsArrayAll.push(item);
			}
			serch();
		}
		
		public function addItemsVector(items:Vector.<GalleryItem>):void{
			clear();
			itemsArrayAll = items;
			serch();
		}
		
		protected function update():void{
			itemsToDraw = new Vector.<GalleryItem>();
			
			if(position == 0){
				disableLeft();
			}else{
				enabledLeft()
			}
			
			if((position + itemsCount) >= itemsArray.length){
				disableRight();
			}else{
				enabledRight();
			}
			
			var cnt:int = 0;
			for (var i:int = position; i < itemsArray.length; i++){
				if(cnt++ < itemsCount){
					var item:GalleryItem = itemsArray[i];
							
					itemsToDraw .push(item);					
				}else{					
					break;
				}
			}
			
			content.Items = itemsToDraw;
		}		
	}
}